package com.locust.component.redis.impl;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;

import com.locust.component.redis.JedisClient;
import com.quan.base.common.utils.serialize.KryoUtil;

public class JedisClientSingle implements JedisClient {
	
	private static Logger logger = LoggerFactory.getLogger(JedisClientSingle.class) ;
	
	/**
	 * 通过Spring声明式方式注入JedisPool连接池
	 */
	private JedisPool jedisPool ;
	
	public JedisPool getJedisPool() {
		return jedisPool;
	}

	public void setJedisPool(JedisPool jedisPool) {
		this.jedisPool = jedisPool;
	}
	
	@Override
	public void set(String key, String value) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			jedis.set(key, value) ;
		} catch(Exception e) {
			logger.error("Cache保存失败[key：{}]", key, e);
		} finally {
			jedis.close();
		}
	}

	@Override
	public String get(String key) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			return jedis.get(key) ;
		} catch(Exception e) {
			logger.error("Cache获取失败[key：{}]", key, e);
			return null ;
		} finally {
			jedis.close();
		}
	}
	
	@Override
	public boolean del(String key) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			jedis.del(key) ;
			return true ;
		} catch(Exception e) {
			logger.error("Cache删除失败[key：{}]", key, e);
			return false ;
		} finally {
			jedis.close();
		}
	}
	@Override
	public void set(byte[] key, byte[] value) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			jedis.set(key, value) ;
		} catch(Exception e) {
			logger.error("Cache保存失败[key：{}]", key, e);
		} finally {
			jedis.close();
		}
	}

	@Override
	public byte[] get(byte[] key) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			return jedis.get(key) ;
		} catch(Exception e) {
			logger.error("Cache获取失败[key：{}]", key, e);
			return null ;
		} finally {
			jedis.close();
		}
	}

	@Override
	public boolean del(byte[] key) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			jedis.del(key) ;
			return true ;
		} catch(Exception e) {
			logger.error("Cache删除失败[key：{}]", key, e);
			return false ;
		} finally {
			jedis.close();
		}
	}
	
	public ScanResult<String> scan(String cursor, ScanParams params) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			return jedis.scan(cursor, params) ;
		} catch(Exception e) {
			logger.error("扫描keys异常", e);
			return null ;
		} finally {
			jedis.close();
		}
	}
	

	@Override
	public void setHash(String key, String field, String value) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			jedis.hset(key, field, value) ;
		} catch(Exception e) {
			logger.error("Cache保存失败[key：{}]", key, e);
		} finally {
			jedis.close();
		}
	}

	@Override
	public String getHash(String key, String field) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			return jedis.hget(key, field) ;
		} catch(Exception e) {
			logger.error("Cache获取失败[key：{}]", key, e);
			return null ;
		} finally {
			jedis.close();
		}
	}
	
	@Override
	public Map<String, String> getHashAll(String key) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			return jedis.hgetAll(key);
		} catch(Exception e) {
			logger.error("Cache获取失败[key：{}]", key, e);
			return null ;
		} finally {
			jedis.close();
		}
	}

	@Override
	public boolean delHash(String key, String field) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			jedis.hdel(key, field) ;
			return true ;
		} catch(Exception e) {
			logger.error("Cache删除失败[key：{}]", key, e);
			return false ;
		} finally {
			jedis.close();
		}
	}

	@Override
	public void setHash(String key, String field, byte[] value) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			jedis.hset(KryoUtil.serializationObject(key), KryoUtil.serializationObject(field), value) ;
		} catch(Exception e) {
			logger.error("Cache保存失败[key：{}]", key, e);
		} finally {
			jedis.close();
		}
	}

	@Override
	public byte[] getHashByte(String key, String field) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			return jedis.hget(KryoUtil.serializationObject(key), KryoUtil.serializationObject(field)) ;
		} catch(Exception e) {
			logger.error("Cache获取失败[key：{}]", key, e);
			return null ;
		} finally {
			jedis.close();
		}
	}

	@Override
	public boolean delHashByte(String key, String field) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			jedis.hdel(KryoUtil.serializationObject(key), KryoUtil.serializationObject(field)) ;
			return true ;
		} catch(Exception e) {
			logger.error("Cache删除失败[key：{}]", key, e);
			return false ;
		} finally {
			jedis.close();
		}
	}
	
	@Override
	public void incr(String key) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			jedis.incr(key) ;
		} catch(Exception e) {
			logger.error("设置缓存失效时间失败[key：{}]", key, e);
		} finally {
			jedis.close();
		}
	}
	
	@Override
	public Long expire(String key, int seconds) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			return jedis.expire(key.toString(), seconds) ;
		} catch(Exception e) {
			logger.error("设置缓存失效时间失败[key：{}]", key, e);
			return null ;
		} finally {
			jedis.close();
		}
	}
	
	@Override
	public Long expire(byte[] key, int seconds) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			return jedis.expire(key, seconds) ;
		} catch(Exception e) {
			logger.error("设置缓存失效时间失败[key：{}]", key, e);
			return null ;
		} finally {
			jedis.close();
		}
	}
	
	@Override
	public Long ttl(String key) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			return jedis.ttl(key.toString()) ;
		} catch(Exception e) {
			logger.error("返回剩余生存时间失败[key：{}]", key, e);
			return null ;
		} finally {
			jedis.close();
		}
	}
	
	@Override
	public Long ttl(byte[] key) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			return jedis.ttl(key) ;
		} catch(Exception e) {
			logger.error("返回剩余生存时间失败[key：{}]", key, e);
			return null ;
		} finally {
			jedis.close();
		}
	}
	
	@Override
	public Boolean exists(String key, int seconds) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			return jedis.exists(key) ;
		} catch(Exception e) {
			logger.error("查询缓存是否存在失败[key：{}]", key, e);
			return null ;
		} finally {
			jedis.close();
		}
	}

	@Override
	public Boolean exists(byte[] key, int seconds) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			return jedis.exists(key) ;
		} catch(Exception e) {
			logger.error("查询缓存是否存在失败[key：{}]", key, e);
			return null ;
		} finally {
			jedis.close();
		}
	}
	
	@Override
	public void flushAll() {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			jedis.flushAll() ;
		} catch (Exception e) {
			logger.error("Cache清空失败：" + e);
		} finally {
			jedis.close();
		}
	}
	
	@Override
	public Long delKeys(String... keys) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			return jedis.del(keys) ;
		} catch (Exception e) {
			logger.error("Cache清空失败：" + e);
		} finally {
			jedis.close();
		}
		return null;
	}
	
	@Override
	public Long delKeys(byte[]... keys) {
		Jedis jedis = null ;
		try {
			jedis = this.jedisPool.getResource() ;
			return jedis.del(keys) ;
		} catch (Exception e) {
			logger.error("Cache清空失败：" + e);
		} finally {
			jedis.close();
		}
		return null;
	}
	
}
